package com.ac.utils;

import cn.hutool.log.Log;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import org.junit.jupiter.api.Test;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;


public class OssUtil {
        private static final String ENDPOINT = "oss-cn-beijing.aliyuncs.com";
        private static final String ACCESS_KEY_ID = "LTAI5tLwr6ujcve4LrgWB5sL"; // KeyId
        private static final String ACCESS_KEY_SECRET = "Bt2STe4aYNmgDCZEycYhbkMrO4kuUO"; // 替换为您的 AccessKeySecret
        private static final String BUCKET_NAME = "peakfit-picture"; // 使用已存在的Bucket名称或确保遵循命名规则
    @Test
    /**
     * 上传图片到OSS
     *
     * @param filePath 文件路径
     * @param objectName 目标存储中的对象键
     * @return 如果上传成功则返回true，否则返回false
     */
    public static String uploadImage(MultipartFile file, String objectName) {
        // 参数校验
        if (file == null || file.isEmpty()) {
            throw new IllegalArgumentException("文件不能为空");
        }
        if (objectName == null || objectName.isEmpty()) {
            throw new IllegalArgumentException("对象名称不能为空");
        }

        OSS ossClient = null;
        try {
            // 创建OSSClient实例
            ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);

            // 检查Bucket是否存在
            if (!ossClient.doesBucketExist(BUCKET_NAME)) {
                throw new RuntimeException("Bucket " + BUCKET_NAME + " 不存在");
            }

            // 上传文件s
            try (InputStream inputStream = file.getInputStream()) {
                // 创建PutObjectRequest
                PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, objectName, inputStream);

                // 设置元数据(可选)
                ObjectMetadata metadata = new ObjectMetadata();
                metadata.setContentType(file.getContentType());
                metadata.setContentLength(file.getSize());
                putObjectRequest.setMetadata(metadata);
                 ossClient.putObject(putObjectRequest);

                // 返回完整的URL
                return "https://" + BUCKET_NAME + "." + ENDPOINT + "/" + objectName;
            }
        } catch (OSSException | ClientException e) {
            throw new RuntimeException("OSS服务异常", e);
        } catch (IOException e) {
            throw new RuntimeException("文件读取失败", e);
        } finally {
            // 确保关闭OSSClient
            if (ossClient != null) {
                try {
                    ossClient.shutdown();
                } catch (Exception e) {
                    throw new RuntimeException("关闭OSSClient失败", e);
                }
            }
        }
    }



    public static String deleteFile(String path) {
        OSS ossClient = null;
        try {
            ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
            ossClient.deleteObject(BUCKET_NAME, path);
            return "删除成功";
        }
        catch (Exception e) {
            System.err.println("Error Message:" + e.getMessage());
            return "删除失败";
        }
    }
    }
